package model;

import java.util.ArrayList;

/**
 * This class implementation maps the slot returned from ladder to actual positions stored  
 * in the Arraylist.
 *
 */
public class LadderGameHelper
{
    /**
     * Returns a slot corresponding to the positions on the grid from the passed position
     * 
     * @param position
     * @return result
     * @throws IndexOutOfBoundsException
     */
    public static Slot getSlotFromPositionString(String position) throws IndexOutOfBoundsException
    {
        int[] positions = getIndexPositionFromString(position);
        
        return Slot.get(positions[0], positions[1]);
    }
    
    /**
     * Returns the index positions on the grid from the passed position
     * 
     * @param position
     * @return result an array of two integers where result[0] = rowIndex and
     *         result[1] = columnIndex
     * @throws IndexOutOfBoundsException
     */
    public static int[] getIndexPositionFromString(String position) throws IndexOutOfBoundsException
    {
        int[] result = new int[2];

        result[0] = Integer.parseInt(position.substring(1, 2)) - 1;

        if (!(0 <= result[0] && result[0] < LadderGame.GRID_ROW_SIZE))
        {
            throw new IndexOutOfBoundsException("Row " + position.substring(1, 2) + " does not exists");
        }

        result[1] = LadderGameHelper.getColumnIndexForChar(position.charAt(0), result[0]);

        return result;
    }    

    /**
     * Returns the column index on the grid from the passed char.
     * 
     * @param colChar
     * @param rowIndex
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static int getColumnIndexForChar(char colChar, int rowIndex) throws IndexOutOfBoundsException
    {
        int result = 0;

        switch (Character.toUpperCase(colChar))
        {
            case 'A':
                result = 0;
                break;
            case 'B':
                result = 1;
                break;
            case 'C':
                result = 2;
                break;
            case 'D':
                result = 3;
                break;
            case 'E':
                result = 4;
                break;
            case 'F':
                result = 5;
                break;
            case 'G':
                result = 6;
                break;
            case 'H':
                result = 7;
                break;
            case 'I':
                result = 8;
                break;
            case 'J':
                result = 9;
                break;
            case 'K':
                result = 10;
                break;
            case 'L':
                result = 11;
                break;
            case 'M':
                result = 12;
                break;
            default:
                throw new IndexOutOfBoundsException("Column " + colChar + " does not exists");
        }

        return result - rowIndex;
    }

    /**
     * Returns the Gamepiece Corresponding to the opponent of the passed player
     * 
     * @param gamePiece
     */
    public static GamePiece getOpponentGamePiece(GamePiece gamePiece) throws Exception
    {
        switch (gamePiece)
        {
            case BLACK:
                return GamePiece.WHITE;
            case WHITE:
                return GamePiece.BLACK;
            default:
                throw new Exception("Wrong function call");
        }
    }
}
